package com.frame.database.dataconver.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

import com.frame.database.dataconver.JDataConversion;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2015
 * @Company:
 * @author 王海锋
 * @CreatedTime:2015-6-25 下午03:09:03
 * @version 1.0
 */

public class DataConversionForOracle implements JDataConversion{

    /**
     * 获取分页Sql
     * 功能:<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2012-6-10 下午12:40:36<br>
     * 说明:<br>
     * 变更:<br>
     */
    public String buildPageSql(String sql, int startIndex, int endIndex) {
        StringBuffer querySQL = new StringBuffer();
        querySQL.append(
                "select * from (select my_table.*,rownum as my_rownum from(").append(
                sql).append(") my_table where rownum<").append(endIndex+1).
                append(") where my_rownum>=").append(startIndex);
        return querySQL.toString();
    }
    /**
     * 将数据转化Oracle数据类型
     * @param value
     * @param type
     * @return
     * @throws ParseException
     */
    public Object valueToDbData(Object value,Class type) throws Exception{
        if(value==null)
            return null;
        if(type == Date.class){
            return new java.sql.Timestamp(((Date)value).getTime());
        }else if(type == byte[].class){
            return (byte[])value;
        }else if(type == char[].class){
            return new String((char[])value);
        }
        else
            return value;
    }
    public Object valueToDbData(Object value){
        if(value==null || "".equals(value))
            return "";
        if(value instanceof java.util.Date){
            return new Timestamp(((java.util.Date)value).getTime());
        }else if(value instanceof char[]){
            return new String((char[])value);
        }
        return value;
    }

}
